#include "xmlreader.h"
#include <QXmlStreamReader>
//#include <exception>
#include <QFile>
//#include <QDebug>
//#include <string>
XmlReader::XmlReader(QIODevice *device)
{

    _xml = new QXmlStreamReader;
    _xml->setDevice(device);

    startReadXml();
}

XmlReader::~XmlReader()
{
    delete _xml;
}

QString XmlReader::getIdName()
{
    return _idName;
}

QString XmlReader::getSymbolName()
{
    return _symbolName;
}

QString XmlReader::getVersion()
{
    return _version;
}

QString XmlReader::getVendor()
{
    return _vendor;
}

QString XmlReader::getCopyright()
{
    return _copyright;
}

QString XmlReader::getActivatorClassName()
{
    return _activatorClassName;
}

QString XmlReader::getActivatorLibraryName()
{
    return _activatorLibraryName;
}

bool XmlReader::getLazyStart()
{
    return  (!_lazyStart.compare("true")) ? true : false;
}

qint32 XmlReader::getRunLevel()
{
    return _runLevel.toInt();
}

QList<QString> XmlReader::getDebpedency()
{
    return _dependency;
}

QString XmlReader::getCode()
{
    QString msg(_code);
    //xml文件中包含有\n\t\n\t \n_ _;
    msg = msg.right(msg.length()-4);
    msg = msg.left(msg.length() -4);

    return msg;
}

QString XmlReader::getFiles()
{
    QString msg(_files);
    //xml文件中包含有\n\t\n\t \n_ _;
    msg = msg.right(msg.length()-4);
    msg = msg.left(msg.length() -4);

    return msg;
}

void XmlReader::startReadXml()
{
    QXmlStreamReader::TokenType token;

    while( !_xml->atEnd() && !_xml->hasError() )
    {
        token = _xml->readNext();
        switch(token)
        {
            case QXmlStreamReader::StartElement:
            {
                if(_xml->name() == "manifest")
                    ReadManifest(_xml);
                else if(_xml->name() == "code")
                {
//                  ReadCode(_xml);
                    _code = _xml->readElementText();
                }
                else if(_xml->name() == "files")
                {
//                    ReadFiles(_xml);
                      _files = _xml->readElementText();
                      ReadFiles(_xml);
                }
                else
                    continue;
            }

            break;
        }

    }
}

void XmlReader::ReadManifest(QXmlStreamReader *xml)
{
    while(xml->readNext())
    {
        if(xml->isEndElement() && xml->name() == "manifest")
            break;
        if(xml->isStartElement())
        {
            if(xml->name() == "name")
                _idName = xml->readElementText();
            else if(xml->name() == "symbolicName")
                _symbolName = xml->readElementText();
            else if(xml->name() == "version")
                _version = xml->readElementText();
            else if(xml->name() == "vendor")
                _vendor = xml->readElementText();
            else if(xml->name() == "copyright")
                _copyright = xml->readElementText();
            else if(xml->name() == "activator")
                ReadActivator(xml);
            else if(xml->name() == "lazyStart")
                _lazyStart = xml->readElementText();
            else if(xml->name() == "runLevel")
                _runLevel = xml->readElementText();
            else if(xml->name() == "dependency")
                ReadDependency(xml);

        }

    }
}

void XmlReader::ReadActivator(QXmlStreamReader *xml)
{
    while(xml->readNext())
    {
        if(xml->isEndElement() && xml->name() == "activator")
            break;
        if(xml->isStartElement())
        {
            if(xml->name() == "class")
                _activatorClassName = xml->readElementText();
            else if(xml->name() == "library")
                _activatorLibraryName = xml->readElementText();
        }
    }
}

void XmlReader::ReadDependency(QXmlStreamReader *xml)
{
    while(xml->readNext())
    {
        if(xml->isEndElement() && xml->name() == "dependency")
            break;
        if(xml->isStartElement())
        {
            if(xml->name() == "symbolicName")
                _dependency.append( xml->readElementText());

        }
    }
}

void XmlReader::ReadCode(QXmlStreamReader *xml)
{
    Q_UNUSED(xml);
}

void XmlReader::ReadFiles(QXmlStreamReader *xml)
{
     Q_UNUSED(xml);
}

